/*!
 * DOMReady module v1.0.0 of jRaiser javascript library
 * http://code.google.com/p/jraiser/
 *
 * Modified from
 *  domready (c) Dustin Diaz 2012 - License MIT
 *
 * Author: Heero.Luo
 * Licensed under MIT license
 *
 * Date: Wed Jul 04 2012 16:05:22 GMT+0800
 */
define(function(require, exports, module) { 'use strict';

module.exports = function (ready) {
	var fns = [], fn, f = false
		, doc = document
		, testEl = doc.documentElement
		, hack = testEl.doScroll
		, domContentLoaded = 'DOMContentLoaded'
		, addEventListener = 'addEventListener'
		, onreadystatechange = 'onreadystatechange'
		, readyState = 'readyState'
		, loaded = doc[readyState] !== 'loading'

	function flush(f) {
		loaded = 1
		while (f = fns.shift()) f()
	}

	doc[addEventListener] && doc[addEventListener](domContentLoaded, fn = function () {
		doc.removeEventListener(domContentLoaded, fn, f)
		flush()
	}, f)

	hack && doc.attachEvent(onreadystatechange, fn = function () {
		if (doc[readyState] !== 'loading') {
			doc.detachEvent(onreadystatechange, fn)
			flush()
		}
	})

	return (ready = hack ?
		function (fn) {
			self != top ?
				loaded ? fn() : fns.push(fn) :
			function () {
				try {
					testEl.doScroll('left')
				} catch (e) {
					return setTimeout(function() { ready(fn) }, 50)
				}
				fn()
			}()
		} :
		function (fn) {
			loaded ? fn() : fns.push(fn)
		}
	)
}();

});